package ar.edu.unq.wannaplay.dtos;

import java.util.LinkedList;
import java.util.List;

import org.joda.time.Interval;

import ar.edu.unq.wannaplay.calendareable.Project;
import ar.edu.unq.wannaplay.client.Client;
import ar.edu.unq.wannaplay.employee.Employee;
import ar.edu.unq.wannaplay.employee.Skill;
import ar.edu.unq.wannaplay.utils.DTOHelper;

public class ProjectDTO extends DTO<Project> {

    private static final long serialVersionUID = 1L;

    private int id;

    private String name;

    private Client client;

    private ClientDTO clientDTO;

    private Interval interval;

    private List<SkillDTO> requirements;

    private List<EmployeeDTO> employees;

    private int manHour;

    public ProjectDTO(final Project project) {
        super();

        id = project.getId();
        name = project.getName();
        interval = project.getInterval();
        client = project.getClient();
        employees = this.generateEmployeesDTO(project.getEmployees());
        requirements = this.generateSkillsDTOs(project.getRequirements());
        manHour = project.getManHour();
    }

    private List<EmployeeDTO> generateEmployeesDTO(final List<Employee> employeesReal) {
        List<EmployeeDTO> employeeDtos = new LinkedList<EmployeeDTO>();
        for (Employee currentEmployee : employeesReal) {
            employeeDtos.add(this.createEmployeeDTO(currentEmployee));
        }
        return employeeDtos;
    }

    protected EmployeeDTO createEmployeeDTO(final Employee employee) {
        return new EmployeeDTO(employee);
    }

    private List<SkillDTO> generateSkillsDTOs(final List<Skill> skillsReal) {
        List<SkillDTO> skillsDtos = new LinkedList<SkillDTO>();
        for (Skill currentSkill : skillsReal) {
            skillsDtos.add(this.createSkillDTO(currentSkill));
        }
        return skillsDtos;
    }

    private SkillDTO createSkillDTO(final Skill currentSkill) {
        return new SkillDTO(currentSkill);
    }

    public ProjectDTO() {
        super();
        // Empty constructor, entirely made for bindingConstruction on
        // presentationModel
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(final Client client) {
        this.client = client;
    }

    public Interval getInterval() {
        return interval;
    }

    public void setInterval(final Interval interval) {
        this.interval = interval;
    }

    public void setEmployees(final List<EmployeeDTO> employees) {
        this.employees = employees;
    }

    public List<EmployeeDTO> getEmployees() {
        return employees;
    }

    public void setRequirements(final List<SkillDTO> requirements) {
        this.requirements = requirements;
    }

    public List<SkillDTO> getRequirements() {
        return requirements;
    }

    @Override
    public Project fromDto() {
        DTOHelper<Employee, EmployeeDTO> helperE = new DTOHelper<Employee, EmployeeDTO>();
        DTOHelper<Skill, SkillDTO> helperS = new DTOHelper<Skill, SkillDTO>();
        List<Employee> realEmployees = helperE.fromDtos(new LinkedList<DTO<Employee>>(this.getEmployees()));
        List<Skill> realSkills = helperS.fromDtos(new LinkedList<DTO<Skill>>(this.getRequirements()));
        Project realProject = new Project(this.getName(), this.getClient(), realSkills, this.getInterval());
        realProject.setId(this.getId());
        realProject.setManHour(this.getManHour());
        realProject.setEmployees(realEmployees);
        return realProject;
    }

    public int getId() {
        return id;
    }

    public void setManHour(final int manHour) {
        this.manHour = manHour;
    }

    public int getManHour() {
        return manHour;
    }

    public String getClientName() {
        return this.getClient().getName();
    }

    public String getClientAddress() {
        return this.getClient().getAddress();
    }

    public String getClientMail() {
        return this.getClient().getEmail();
    }

    public String getClientTown() {
        return this.getClient().getTown();
    }

    public void setClientName(final String aName) {
        this.getClient().setName(aName);
    }

    public void setClientTown(final String town) {
        this.getClient().setTown(town);
    }

    public void setClientAddress(final String address) {
        this.getClient().setAddress(address);
    }

    public void setClientMail(final String email) {
        this.getClient().setEmail(email);
    }

    public void setId(final int id) {
        this.id = id;
    }

    public void setClientDTO(final ClientDTO projectClient) {
        clientDTO = projectClient;
    }

    public ClientDTO getClientDTO() {
        return clientDTO;
    }

}
